package com.autcar.training.service;

import com.autcar.training.VO.OrderRequest;
import com.autcar.training.dao.*;
import com.autcar.training.entity.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @ProjectName: training
 * @Package: com.autcar.training.service
 * @ClassName: OrderServiceImpl
 * @Author: YHHHH
 * @Description: 订单服务实现类
 * @Date: 2020/8/28 14:54
 * @Version: 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private FeedBackMapper feedBackMapper;

    @Resource
    private WorkerMapper workerMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private InsuranceMapper insuranceMapper;



    //删除订单
    @Override
    public int deleteOrder(Integer id) {
        return orderMapper.deleteById(id);
    }

    //修改订单
    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateById(order);
    }

    // 查询所有的订单
    public List<Order> findAllOrder(){
        return orderMapper.findAllOrder();
    }

    //增加一个订单
    @Override
    public int addOrder(OrderRequest orderRequest){

        Worker worker = workerMapper.selectOne(new QueryWrapper<Worker>().like("name", orderRequest.getWorkerName()));
        Goods goods = goodsMapper.selectOne(new QueryWrapper<Goods>().like("name", orderRequest.getGoodsName()));
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().like("name",orderRequest.getCustomerName()));

        //时间戳从秒
        int totalId = Integer.parseInt(new SimpleDateFormat("dHHmmss").format(new Date()));

        Order one=new Order();
        one.setId(totalId);
        one.setGid(goods.getId());
        one.setMoney(goods.getPrice());
        one.setWid(worker.getId());
        one.setCid(customer.getId());
        one.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        one.setState(0);
        orderMapper.insert(one);

        //添加保险信息
        Insurance insurance = new Insurance();
        insurance.setOid(totalId);
        insuranceMapper.insert(insurance);
        return 1;
    }

    @Override
    public int updateStatusFalse(Integer id) {
        Order order=new Order();
        order.setId(id);
        order.setState(0);
        return orderMapper.updateById(order);
    }

    @Override
    public int updateStatusTrue(Integer id) {
        Order order=new Order();
        order.setId(id);
        order.setState(1);
        return orderMapper.updateById(order);
    }

    @Override
    public List<Order> findByCustomerName(String customerName) {
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().like("name",customerName));

        return orderMapper.findByCustomerName(customer.getId());
    }


    //通过订单号查看订单
    public Order findOrderById(Integer id){
        return orderMapper.findOrderById(id);
    }

}
